package com.atguigu.tingshu.user.strategy.impl;



import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;

import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.UpdateUserPaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class UserVipServiceStrategy implements UpdateUserPaidRecordStrategy {




        @Autowired
        private UserInfoMapper userInfoMapper;
        @Autowired
        private UserVipServiceMapper userVipServiceMapper;

        @Autowired
        private VipServiceConfigMapper vipServiceConfigMapper ;  //vip的配置信息
    @Override
    public void updateUserPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //1.判断是否重复消费
        LambdaQueryWrapper<UserVipService> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo());
        UserVipService userVipService = userVipServiceMapper.selectOne(lambdaQueryWrapper);
        if (userVipService != null) { //如果有值就是重复提交了
            return;
        }

        //2.查询vip配置的信息
        userVipService = new UserVipService();
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        Long userId = AuthContextHolder.getUserId();
        userVipService.setUserId(userId);

        /*
         * 计算开始时间的逻辑
         * 判断是否为vip,是的话 查证是否过期 没过期 会员过期的时间 就是开始的时间
         * 不是vip 系统的当前时间 就是vip的开始时间
         * */

        //远程调用 查询用户信息
        UserInfo userInfo = userInfoMapper.selectById(userId);

        Date startTime = new Date();
        if (userInfo.getIsVip().equals(1) && startTime.before(userInfo.getVipExpireTime())) { //判断此时是vip
            startTime = userInfo.getVipExpireTime();
        } else {
            userVipService.setStartTime(startTime);
        }

        //计算vip的过期时间   首先查询vip的过去时间 得先知道是哪一个用户的vip时间
        Long vipServerConfigId = userPaidRecordVo.getItemIdList().get(0);
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipServerConfigId);

        /**
         * 关于日期相关计算的操作，不需要自己手动计算，可以直接使用日期工具包：
         * 1、joda-time
         * 2、hutool
         */
        //现在设置vip的过期时间
        Date expireTime = new DateTime(startTime).plusMonths(vipServiceConfig.getServiceMonth()).toDate();
        userVipService.setExpireTime(expireTime);
        userVipServiceMapper.insert(userVipService);

        //用户购买了vip vip的配置信息 要改变一下
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(expireTime);
        userInfoMapper.updateById(userInfo);

    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_VIP;
    }
}